package com.feihong.txgw.adapter.delivery;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.adapter.base.animation.ScaleInAnimation;
import com.alibaba.fastjson.JSON;
import com.feihong.txgw.R;
import com.feihong.txgw.adapter.Holder.DeliveryAdrViewHolder;
import com.feihong.txgw.anim.TxListAnimation;
import com.feihong.txgw.inter.listener.delivery_adr.DeliveryAdrDeleteAdrListener;
import com.feihong.txgw.inter.listener.delivery_adr.DeliveryAdrEditListener;
import com.feihong.txgw.model.bean.deliveryAdr.DeliveryAdrEntity;
import com.feihong.txgw.model.bean.kaika.KaikaYuYueOrderInfo;
import com.feihong.txgw.ui.activity.kaika.KaikaNewPackageActivity;
import com.feihong.txgw.ui.activity.user.MyDeliveryAdrActivity;
import com.feihong.txgw.utils.PreferenceTools;
import com.feihong.txgw.utils.ToastUtils;
import com.feihong.txgw.utils.adress.StringEmpty;
import com.feihong.txgw.utils.kaika.KaikaIntentType;


import java.util.ArrayList;

/**
 * @author wangziheng
 * @name txgw_app
 * @class name：com.feihong.txgw.adapter
 * @class describe:
 * @time 2017/7/4 16:06
 * @change
 * @chang time
 * @class describe
 */
public class DeliveryAdrAdapter extends RecyclerView.Adapter<DeliveryAdrViewHolder> {

    private MyDeliveryAdrActivity mContext;
    private ArrayList<DeliveryAdrEntity> mDeliverAdrs;

    private TxListAnimation mTxAnimation;
    public DeliveryAdrAdapter(Context context) {
        mContext = (MyDeliveryAdrActivity) context;
        mDeliverAdrs = new ArrayList<>();
        mKaikaYuYueOrderInfo = KaikaYuYueOrderInfo.getInstance();
        mTxAnimation = new TxListAnimation();
        mTxAnimation.addAnimation(new ScaleInAnimation(0.8f));
        mTxAnimation.setFirstOnly(true);
    }

    @Override
    public void onViewAttachedToWindow(@NonNull DeliveryAdrViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        mTxAnimation.setHolder(holder);
    }

    public void setAdrList(ArrayList<DeliveryAdrEntity> deliveryAdrEntities) {
        mDeliverAdrs = deliveryAdrEntities;
        notifyDataSetChanged();
    }

    public void setClearList() {
        mDeliverAdrs.clear();
        notifyDataSetChanged();
    }

    private String isSelectAdr;
    private KaikaYuYueOrderInfo mKaikaYuYueOrderInfo;

    public void setSelectOrderAdr(String isSelect) {
        isSelectAdr = isSelect;
    }

    @Override
    public DeliveryAdrViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DeliveryAdrViewHolder(LayoutInflater.from(mContext).inflate(R.layout.delivery_adr_item_layout, parent, false));
    }

    @Override
    public void onBindViewHolder(DeliveryAdrViewHolder holder, int position) {
        final DeliveryAdrEntity mEntity = mDeliverAdrs.get(position);
        if (isSelectAdr.equals(KaikaIntentType.ADDRESS_INTENT_TYPE_ITEN_CLICK_BACK)) {
            holder.mDeliveryAdrItemSelect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(StringEmpty.isEmpty(mEntity.getAddress()) || StringEmpty.isEmpty(mEntity.getLocation_info())){
                        ToastUtils.getInstance().showShortToast("地址信息不全，请先补全地址信息");
                    }else {
                        PreferenceTools.putString(MyDeliveryAdrActivity.SELECT_ADR_KEY, JSON.toJSONString(mEntity));
                        Intent intent = mContext.getIntent();
                        Bundle bundle = new Bundle();
                        bundle.putParcelable(KaikaIntentType.TERMINAL_ORDER_SELECT_ADR_KEY, mEntity);
                        intent.putExtras(bundle);
                        mContext.setResult(Activity.RESULT_OK, intent);
                        mContext.finish();
                    }
                }
            });
        } else if (isSelectAdr.equals(KaikaIntentType.ADDRESS_INTENT_TYPE_ITEM_CLICK_NEXT_PACKAGE)) {
            holder.mDeliveryAdrItemSelect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(StringEmpty.isEmpty(mEntity.getAddress()) || StringEmpty.isEmpty(mEntity.getLocation_info())){
                        ToastUtils.getInstance().showShortToast("地址信息不全，请先补全地址信息");
                    }else {
                        Intent intent = new Intent(mContext, KaikaNewPackageActivity.class);
                        PreferenceTools.putString(MyDeliveryAdrActivity.SELECT_ADR_KEY, JSON.toJSONString(mEntity));
                        String mAdress = (StringEmpty.isEmpty(mEntity.getAddress()) ? "" : mEntity.getAddress())
                                + (StringEmpty.isEmpty(mEntity.getHouse_num()) ? "" : mEntity.getHouse_num());
                        mKaikaYuYueOrderInfo.setmAddressId(mEntity.getAddr_id());
                        mKaikaYuYueOrderInfo.setmAddress(mAdress);
                        intent.putExtra(KaikaIntentType.PACKAGE_INTENT_TYPE_KEY, KaikaIntentType.INTENT_FROM_ADDRESS_PAGE);
                        mContext.startActivity(intent);
                    }
                }
            });
        }
        if ("1".equals(mDeliverAdrs.get(position).getIs_default())) {
            holder.mDeliveryAdrSelect.setVisibility(View.VISIBLE);
            holder.mDeliveryAdrUserName.setTextColor(mContext.getResources().getColor(R.color.delivery_adr_select_item_user_info));
            holder.mDeliveryAdrUsrPhone.setTextColor(mContext.getResources().getColor(R.color.delivery_adr_select_item_user_info));

        } else {
            holder.mDeliveryAdrSelect.setVisibility(View.GONE);
            holder.mDeliveryAdrUserName.setTextColor(mContext.getResources().getColor(R.color.delivery_adr_item_user_info));
            holder.mDeliveryAdrUsrPhone.setTextColor(mContext.getResources().getColor(R.color.delivery_adr_item_user_info));

        }

        String str;
        if (mDeliverAdrs.get(position).getConsignee().length() > 3) {
            str = mEntity.getConsignee().substring(0, 3) + "..";
        } else {
            str = mEntity.getConsignee();
        }
        holder.mDeliveryAdrUserName.setText(str);
        String userAdr = ((StringEmpty.isEmpty(mEntity.getAddress()) ? "" : mDeliverAdrs.get(position).getAddress())
                + (StringEmpty.isEmpty(mEntity.getHouse_num()) ? "" : mDeliverAdrs.get(position).getHouse_num()));
        holder.mDeliveryAdrUserAdr.setText(userAdr);
        holder.mDeliveryAdrUsrPhone.setText(mEntity.getMobile());
        /**
         * 删除地址
         */
        holder.mDeliveryAdrDeleteAdrItemImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mDeliveryAdrDeleteAdrListener.onDeleteAdr(mEntity);
            }
        });

        /**
         * 编辑地址
         */
        holder.mDeliveryAdrEditAdrItemImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mDeliveryAdrEditListener.onEditAdr(mEntity);
//                EventBus.getDefault().post(new EditDeliveryAdrMessageEnevent(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDeliverAdrs.size();
    }

    private DeliveryAdrEditListener mDeliveryAdrEditListener;
    private DeliveryAdrDeleteAdrListener mDeliveryAdrDeleteAdrListener;

    /**
     * 编辑地址监听
     *
     * @param deliveryAdrEditListener
     */
    public void setDeliveryAdrEditListener(DeliveryAdrEditListener deliveryAdrEditListener) {
        mDeliveryAdrEditListener = deliveryAdrEditListener;
    }

    /**
     * 删除地址监听
     *
     * @param deliveryAdrDeleteListener
     */
    public void setDeliveryAdrDeleteListener(DeliveryAdrDeleteAdrListener deliveryAdrDeleteListener) {
        mDeliveryAdrDeleteAdrListener = deliveryAdrDeleteListener;
    }
}
